package org.academic.texteditor.gui.component.menu;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import org.academic.texteditor.gui.form.AbstractInternalFrame;
import org.academic.texteditor.gui.plugin.Plugin;
import org.academic.texteditor.gui.util.OpenWindowListener;
import org.academic.texteditor.helper.MessageHelper;

public class MenuFactory {

	private static final MessageHelper msg = MessageHelper.getInstance();
	
	public static JMenu getApplication() {
		final String menuName = "application";
		
		JMenu menu = MenuManager.getInstance().getMenu(menuName);
		if (menu != null) {
			return menu;
		}
		
		menu = createMenu(menuName, "menu.application");
		menu.addSeparator();
		createMenuItem(menu, "exit", "menu.application.exit", new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		MenuManager.getInstance().addMenu(menu);
		
		return menu;
	}

	public static JMenu getEdit() {
		final String menuName = "edit";
		
		JMenu menu = MenuManager.getInstance().getMenu(menuName);
		if (menu != null) {
			return menu;
		}
		
		menu = createMenu(menuName, "menu.edit");
		menu.setVisible(false);
		
		MenuManager.getInstance().addMenu(menu);
		
		return menu;
	}

	public static JMenu getInsert() {
		final String menuName = "insert";
		
		JMenu menu = MenuManager.getInstance().getMenu(menuName);
		if (menu != null) {
			return menu;
		}
		
		menu = createMenu(menuName, "menu.insert");
		menu.setVisible(false);
		
		MenuManager.getInstance().addMenu(menu);
		
		return menu;
	}

	public static JMenu getFormat() {
		final String menuName = "format";
		
		JMenu menu = MenuManager.getInstance().getMenu(menuName);
		if (menu != null) {
			return menu;
		}
		
		menu = createMenu(menuName, "menu.format");
		menu.setVisible(false);
		
		MenuManager.getInstance().addMenu(menu);
		
		return menu;
	}

	public static JMenu getReference() {
		final String menuName = "reference";
		
		JMenu menu = MenuManager.getInstance().getMenu(menuName);
		if (menu != null) {
			return menu;
		}
		
		menu = createMenu(menuName, "menu.reference");
		menu.setVisible(false);
		
		MenuManager.getInstance().addMenu(menu);
		
		return menu;
	}
	
	public static JMenu createMenu(String name, String key) {
		MenuManager manager = MenuManager.getInstance();
		JMenu menu = manager.getMenu(name);
		if (menu != null) {
			return menu;
		}
		
		menu = new JMenu(msg.getMessage(key));
		menu.setName(name);
		menu.setMnemonic(msg.getMessage(key + "_mnemonic").charAt(0));

		manager.addMenu(menu);
		
		return menu;
	}
	
	public static JMenuItem createMenuItem(JComponent menu, String name, String key, AbstractInternalFrame frame) {
		return createMenuItem(menu, -1, name, key, new OpenWindowListener(frame));
	}

	public static JMenuItem createCheckBoxMenuItem(JComponent menu, String name, String key, AbstractInternalFrame frame) {
		return createCheckBoxMenuItem(menu, -1, name, key, new OpenWindowListener(frame));
	}
	
	public static JMenuItem createMenuItem(JComponent menu, int index, String name, String key, final Plugin plugin) {
		return createMenuItem(menu, index, name, key, new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				plugin.execute();
			}});
	}

	public static JCheckBoxMenuItem createCheckBoxMenuItem(JComponent menu, int index, String name, String key, final Plugin plugin) {
		return createCheckBoxMenuItem(menu, index, name, key, new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				plugin.execute();
			}});
	}
	
	public static JMenuItem createMenuItem(JComponent menu, String name, String key, ActionListener actionListener) {
		return createMenuItem(menu, -1, name, key, actionListener);
	}

	public static JMenuItem createCheckBoxMenuItem(JComponent menu, String name, String key, ActionListener actionListener) {
		return createCheckBoxMenuItem(menu, -1, name, key, actionListener);
	}
	
	public static JMenuItem createMenuItem(JComponent menu, int index, String name, String key, ActionListener actionListener) {
		return createMenuItem(menu, index, name, key, false, actionListener);
	}

	public static JCheckBoxMenuItem createCheckBoxMenuItem(JComponent menu, int index, String name, String key, ActionListener actionListener) {
		return (JCheckBoxMenuItem) createMenuItem(menu, index, name, key, true, actionListener);
	}
	
	private static JMenuItem createMenuItem(JComponent menu, int index, String name, String key, boolean check, ActionListener actionListener) {
		JMenuItem menuItem;
		if (check) {
			menuItem = new JCheckBoxMenuItem(msg.getMessage(key));
		} else {
			menuItem = new JMenuItem(msg.getMessage(key));
		}
		menuItem.setName(name);
		menuItem.setMnemonic(msg.getMessage(key + "_mnemonic").charAt(0));
		menuItem.addActionListener(actionListener);
		
		if (index == -1) {
			menu.add(menuItem);
		} else {
			menu.add(menuItem, index);
		}
		
		return menuItem;
	}

	public static void removeMenuItem(JMenu menu, String name) {
		Component[] components = menu.getMenuComponents();
		for (Component component : components) {
			if (component.getName().equals(name)) {
				menu.remove(component);
				break;
			}
		}
	}
	
}
